package com.mtl.inmigrant.shoes;

import java.io.IOException;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.StrictMode;
import android.preference.PreferenceManager;
import android.view.View;
import android.webkit.WebView;
import android.webkit.WebViewClient;

import com.google.api.client.auth.oauth2.draft10.AccessTokenResponse;
import com.google.api.client.googleapis.auth.oauth2.draft10.GoogleAccessTokenRequest.GoogleAuthorizationCodeGrant;
import com.google.api.client.googleapis.auth.oauth2.draft10.GoogleAuthorizationRequestUrl;
import com.google.api.client.http.javanet.NetHttpTransport;
import com.google.api.client.json.jackson.JacksonFactory;
import com.mtl.inmigrant.shoes.preferences.SharedPreferencesImmigrant;


public class OAuthActivity extends android.app.Activity {

	private SharedPreferences prefs;
	//SharedPreferencesImmigrant credentialStore;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		this.prefs = PreferenceManager.getDefaultSharedPreferences(this);
	    if (android.os.Build.VERSION.SDK_INT > 9) {
	        StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
	        StrictMode.setThreadPolicy(policy);
	      }


		// new OAuthRequestTokenTask(this).execute();
	}

	@Override
	protected void onResume() {
		super.onResume();
		WebView webview = new WebView(this);
		webview.setVisibility(View.VISIBLE);
		webview.getSettings().setJavaScriptEnabled(true);
		setContentView(webview);
		
		String googleAuthorizationRequestUrl = new GoogleAuthorizationRequestUrl(
				SharedPreferencesImmigrant.CLIENT_ID,
				SharedPreferencesImmigrant.REDIRECT_URI,
				SharedPreferencesImmigrant.SCOPE).build();

		webview.setWebViewClient(new WebViewClient() {
 
			@Override
			public void onPageFinished(WebView view, String url) {

				if (url.startsWith(SharedPreferencesImmigrant.REDIRECT_URI)) {
					try {
						if (url.indexOf("code=") != -1) {
							// Url is like http://localhost/?code=4/Z5DgC1IxNL-muPsrE2Sjy9zQn2pF
							String code = url.substring(SharedPreferencesImmigrant.REDIRECT_URI.length() + 7,url.length());

							AccessTokenResponse accessTokenResponse = new GoogleAuthorizationCodeGrant(
									new NetHttpTransport(),
									new JacksonFactory(),
									SharedPreferencesImmigrant.CLIENT_ID,
									SharedPreferencesImmigrant.CLIENT_SECRET,
									code, SharedPreferencesImmigrant.REDIRECT_URI)
									.execute();

							SharedPreferencesImmigrant credentialStore = SharedPreferencesImmigrant.getInstance(prefs);
							credentialStore.write(accessTokenResponse);
							view.setVisibility(View.INVISIBLE);
							startActivity(new Intent(
									OAuthActivity.this,
									MtlInmigrantShoesActivity.class));
							finish();
						} else if (url.indexOf("error=") != -1) {
							view.setVisibility(View.INVISIBLE);
							SharedPreferencesImmigrant.getInstance(prefs)
									.clearCredentials();
							startActivity(new Intent(
									OAuthActivity.this,
									SummaryMainActivity.class));
							finish();
						}

					} catch (IOException e) {
						e.printStackTrace();
					}
				}
			}
		});

		webview.loadUrl(googleAuthorizationRequestUrl);
	}
}
